package com.compass.doctor.service;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.Nullable;

import com.compass.doctor.R;

/**
 * Description： 悬浮窗service
 * Date:2024/11/7 14:37
 */
public class FloatingWindowService extends Service {
    private WindowManager windowManager;
    private View floatingView;


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();


// 创建悬浮窗口的布局
        floatingView = LayoutInflater.from(this).inflate(R.layout.floating_window, null);
        // 获取 WindowManager
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        // 设置悬浮窗口的参数
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
                        ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
                        : WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        // 设置悬浮窗口的位置和大小
        params.gravity = Gravity.TOP | Gravity.START;
        params.x = 0;
        params.y = 100;
        // 添加悬浮窗口的布局
        windowManager.addView(floatingView, params);
        // 添加拖动和关闭功能
        floatingView.setOnTouchListener(new FloatingOnTouchListener(params));
        floatingView.findViewById(R.id.close_button).setOnClickListener(new FloatingOnClickListener());




    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        // 移除悬浮窗口的布局
        if (floatingView != null) {
            windowManager.removeView(floatingView);
        }
    }

    // 实现拖动功能
    private class FloatingOnTouchListener implements View.OnTouchListener {
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;
        private WindowManager.LayoutParams params;
        FloatingOnTouchListener(WindowManager.LayoutParams params) {
            this.params = params;
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    // 记录触摸点的位置和悬浮窗口的位置
                    initialX = params.x;
                    initialY = params.y;
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    return true;
                case MotionEvent.ACTION_UP:
                    // 拖动结束，更新悬浮窗口的位置
                    int newX = initialX + (int) (event.getRawX() - initialTouchX);
                    int newY = initialY + (int) (event.getRawY() - initialTouchY);
                    params.x = newX;
                    params.y = newY;
                    windowManager.updateViewLayout(floatingView, params);
                    return true;
                case MotionEvent.ACTION_MOVE:
                    // 拖动中，更新悬浮窗口的位置
                    int newX1 = initialX + (int) (event.getRawX() - initialTouchX);
                    int newY1 = initialY + (int) (event.getRawY() - initialTouchY);
                    params.x = newX1;
                    params.y = newY1;
                    windowManager.updateViewLayout(floatingView, params);
                    return true;
            }
            return false;
        }
    }

    // 实现关闭功能
    private class FloatingOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            stopSelf();
        }
    }


}
